package absc.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.MultiPageEditorPart;

public class SWOTEditor extends MultiPageEditorPart {

	private TextEditor teSource;
	private SWOTEditorDesignPage designPage;
	
	public SWOTEditor() {
		// TODO Auto-generated constructor stub
	}
	
	public void init(IEditorSite site, IEditorInput input) throws PartInitException{
		if (!(input instanceof IFileEditorInput))
			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		
	    super.init(site, input);	    
	}

	@Override
	protected void createPages() {
		createSourcePage();
		createDesignPage();
		updateTitle();
	}

	private void updateTitle() {
		IEditorInput input = getEditorInput();
		setPartName(input.getName());
		setTitleToolTip(input.getToolTipText());
	}

	private void createDesignPage() {
		try{
			designPage = new SWOTEditorDesignPage();
			int index = addPage(this.designPage, getEditorInput());
			setPageText(index, "Design");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	private void createSourcePage() {
		try{
			this.teSource = new TextEditor();
			int index = addPage(this.teSource, getEditorInput());
			setPageText(index, "Source");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void doSave(IProgressMonitor arg0) {
		this.teSource.doSave(arg0);
		this.designPage.refresh();
	}

	@Override
	public void doSaveAs() {
		this.teSource.doSaveAs();
		setInput(this.teSource.getEditorInput());
		updateTitle();
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	public void setFocus(){
		switch (getActivePage()) {
			case 0:
				this.teSource.setFocus();				
		        break;
		    case 1:		    			    	
		    	this.designPage.setFocus();
		        break;
		}		
	}
}
